home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / C++ AppleLink Messages / CPlus.Dev$ 6⁄8⁄90 / 0141-Array of objects in -Jun90 < prev    next >
Encoding:
Text File  |  1990-06-08  |  2.3 KB  |  73 lines  |  [TEXT/GEOL]

  1. Item    4194850                         7-June-90        21:44PDT
  2.  
  3. From:   M.DANIEL                        Daniel Scientific, M Daniel,PRT
  4.  
  5. To:     CPLUS.APPLE$                    C++ Interest List--Apple Employees
  6.         CPLUS.DEV$                      C++ Interest List--Developers
  7.  
  8. Sub:    Array of objects in a handle
  9.  
  10. Hi! Guys and Gals,
  11.  
  12. I have an array of objects in a handle.  Here's how I do it.
  13.  
  14. A temporary pointer (TP) is a pointer to a memory location allocated in the
  15. heap as a relocatable block.  The rule is: a TP is only valid as long as memory
  16. is not moved.
  17.  
  18. The problem is to get the C++ compiler to accept a TP as an object of some
  19. class.  I overload the "new" operator like this:
  20.  
  21.    TP = new ( handle, offset ) class;
  22.  
  23. to create the object.  This version of "new" would simply deference the handle,
  24. add the offset to it, and return this result.
  25. Now there is a problem here.  I have my TP and the compiler accepts it as an
  26. object, but the compiler will also call a constructor on my object everytime I
  27. use this version of new.  This is good the first time I call "new", but bad
  28. thereafter.  I'll re-initialize my object everytime I access it.  The answer is
  29. to use constructor that does nothing.  So the first time I access the object:
  30.  
  31.     TP = new ( handle, offset ) class( initMe );
  32.  
  33. and thereafter:
  34.  
  35.     TP = new ( handle, offset ) class( ignore );
  36.  
  37. So now I hide it all behind an array access interface.
  38.  
  39. class objarray {
  40.     public:
  41.         obj& operator [] ( long );
  42.     private:
  43.         Handle h;
  44. };
  45.  
  46. obj& objarray::operator[] ( long i )  {
  47.  
  48.     TP = new ( h, i ) obj(ignore);
  49.  
  50.     return *TP;
  51. }
  52.  
  53. Add the rest of the details and Taa Daa!
  54. An array of objects in a handle, but accessed in source code like any array.
  55.  
  56. For the index/offset, I used another class, arrayIndex.  It allowed me to
  57. overload array index arithmetic to conform to the standard array index
  58. arithmetic.  There is one more detail to mention.  The dead space between array
  59. objects that allow them to all begin on long word boundaries.  The size of an
  60. object may not the the distance between in an array.
  61.  
  62. What do you think?
  63.  
  64.  
  65. Michael J. Daniel
  66. Daniel Scientific
  67. AppleLink: M.Daniel
  68.  
  69.  
  70. PS: You could also overload to call MoreMasters(), and allocate the array as
  71. individual handles, but I haven't tried it yet.
  72.  
  73.